import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.io.*;
import javax.swing.*;

public class GUI implements Serializable {
	
	final CommandManager CmdMan;
	final Player player;
	private BattleManager BattleMan;
	private ConversationManager ConvoMan;
	public JTextArea feedbackWindow;

	public GUI(CommandManager a, Player b) {
		CmdMan = a;
		player = b;
		feedbackWindow = new JTextArea();
	}
	
	public void init() {
		
		final JFrame MainWindow = new JFrame("Adventure");
		MainWindow.setSize(1280, 720);
		MainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		final JPanel panel = new JPanel();
		final JPanel panel2 = new JPanel();
		final JPanel panel3 = new JPanel();
		final JPanel panel4 = new JPanel();
		final StatusWindow stats = new StatusWindow();
		final JTextField inputarea = new JTextField(15);
		final LocationWindow outputarea = new LocationWindow(15, 30);
		final JTextArea feedback = new JTextArea(15, 45);
		final InventoryWindow inventoryscreen = new InventoryWindow();
		final MoveListWindow movelist = new MoveListWindow();
		final QuestWindow questWindow = new QuestWindow();
		questWindow.setSize(10, 20);
		inventoryscreen.setSize(10, 20);
		
		BattleMan = new BattleManager(feedback, movelist);
		ConvoMan = new ConversationManager(feedback);

		inventoryscreen.setEditable(false);
		movelist.setEditable(false);
		movelist.setVisible(true);
		questWindow.setEditable(false);
		questWindow.setVisible(true);
		stats.setEditable(false);
		feedback.setEditable(false);
		feedback.setLineWrap(true);
		feedback.setWrapStyleWord(true);
		outputarea.setEditable(false);
		outputarea.setLineWrap(true);
		outputarea.setWrapStyleWord(true);
		final JScrollPane scroll = new JScrollPane(outputarea);
		final JScrollPane scroll2 = new JScrollPane(feedback);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scroll2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		final JButton button = new JButton("Submit");
		
		if (player.isInCombat() == false) {
		
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
	          {
				
				feedback.setText("");
				feedback.append("> " + inputarea.getText() + "\n\n");
					if (player.isInCombat() == false && player.isAsleep() == false) {
	              CmdMan.ParseCmd(inputarea.getText(), player, feedback, outputarea, inventoryscreen, inputarea);
					} else if (player.isInCombat() == true){
						BattleMan.ParseCmd(inputarea.getText());
						stats.Refresh(player);
						movelist.Refresh(player);
					} else if (player.isAsleep() == true) {
						ConvoMan.parseCmd(inputarea.getText(), outputarea, player);
					}
					
				  inputarea.setText("");
	              outputarea.Refresh(player);
	              stats.Refresh(player);
	              movelist.Refresh(player);
	              questWindow.Refresh(player);
				  scroll.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){
					  public void adjustmentValueChanged(AdjustmentEvent e){
					  outputarea.select(outputarea.getHeight()+10000,0);
					  }});
	          }
	      });
		
		} else {
			

			
			
			
		}
		
		panel.add(inputarea);
		panel.add(button);
		panel3.add(inventoryscreen);
		panel3.add(questWindow);
		panel4.add(stats);
		panel4.add(movelist);
		panel2.add(scroll2);
		MainWindow.add(panel4, BorderLayout.EAST);
		MainWindow.add(panel, BorderLayout.SOUTH);
		MainWindow.add(panel3, BorderLayout.WEST);
		MainWindow.add(scroll, BorderLayout.NORTH);
		MainWindow.add(panel2, BorderLayout.CENTER);
		
		this.feedbackWindow = feedback;

		MainWindow.setVisible(true);
		
		player.getCurrentLocation().Display(outputarea);
		inventoryscreen.Refresh(player);
		stats.Refresh(player);
		movelist.Refresh(player);
		questWindow.Refresh(player);
		
	}
	
	public BattleManager getBattleMan() {
		return BattleMan;
	}
	
	public ConversationManager getConvoMan() {
		return ConvoMan;
	}
	
	public JTextArea getFeedbackWindow() {
		return feedbackWindow;
	}
	
	public Player getPlayer() {
		return player;
	}
	
}
	

